package sunyu.tools.selenium;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class SeleniumTools {

    private static final Logger logger = LoggerFactory.getLogger(Thread.currentThread().getClass());

    private WebDriver driver = new FirefoxDriver();// 定义火狐WebDriver
    private Actions action = new Actions(driver);// 定义全局动作

    /**
     * 关闭浏览器
     */
    public void close() {
        action.release();// 鼠标释放
        driver.close();// 关闭浏览器
    }

    /**
     * 获得一个WebElement
     *
     * @param xpath
     *
     * @return
     */
    public WebElement getWebElement(String xpath) {
        return getWebElement(xpath, 10);
    }

    /**
     * 获得一个WebElement
     *
     * @param xpath
     * @param timeOutInSeconds
     *
     * @return
     */
    public WebElement getWebElement(String xpath,
                                    long timeOutInSeconds) {
        return new WebDriverWait(driver, timeOutInSeconds, 1000).until(new ExpectedCondition<WebElement>() {
            @Override
            public WebElement apply(WebDriver driver) {
                return driver.findElement(By.xpath(xpath));
            }
        });
    }

    /**
     * 获得一个WebElement
     *
     * @param xpath
     * @param errMsg           如果遇到错误的描述
     * @param timeOutInSeconds
     *
     * @return
     */
    public WebElement getWebElement(String xpath,
                                    String errMsg,
                                    long timeOutInSeconds) {
        try {
            return getWebElement(xpath, timeOutInSeconds);
        } catch (Exception e) {
            e.printStackTrace();
            if (StringUtils.isNotBlank(errMsg)) {
                logger.error(errMsg);
            }
        }
        return null;
    }

    /**
     * 最大化窗口
     */
    public void maxWindow() {
        driver.manage().window().maximize();
    }

    /**
     * 打开网页
     *
     * @param url
     */
    public void openUrl(String url) {
        driver.get(url);
    }

    /**
     * 模拟鼠标移动到element这个地方
     *
     * @param element
     */
    public void moveToElement(WebElement element) {
        action.moveToElement(element).perform();
    }

    /**
     * 输入字符
     *
     * @param xpath
     * @param text
     */
    public void sendKeys(String xpath,
                         String text) {
        WebElement element = getWebElement(xpath);
        element.clear();
        element.sendKeys(text);
    }

    /**
     * 点击元素
     *
     * @param xpath
     */
    public void click(String xpath) {
        getWebElement(xpath).click();
    }

}
